package com.example.carrierapp.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.example.lb.fingerdemo.R;

/**
 * 文字弹出框
 */
public class DialogUtils {
    public static final int DEFAULT = 0; //默认样式
    public static final int LOADING = 1; //进度样式

    public static class Builder {
        private Context context = null;
        private String message = "";
        private String positive = "确定";
        private Integer positiveColor;
        private CallbackListener positiveCallbackListener = null;
        private String negative = "取消";
        private Integer negativeColor;
        private boolean isOutTouch = true;
        private CallbackListener negativeCallbackListener = null;
        private int themeResId = 0;
        private int type = DEFAULT;
        private boolean onlyPositive;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setOnlyPositive() {
            onlyPositive = true;
            return this;
        }

        public Builder setOutTouch(boolean isOutTouch) {
            this.isOutTouch = isOutTouch;
            return this;
        }

        public Builder setPositiveButton(String positive, Integer positiveColor,
                                         CallbackListener positiveCallbackListener) {
            this.positive = positive;
            this.positiveColor = positiveColor;
            this.positiveCallbackListener = positiveCallbackListener;
            return this;
        }

        public Builder hideNegativeButton()
        {
            return this;
        }

        public Builder setNegativeButton(String negative, Integer negativeColor,
                                         CallbackListener negativeCallbackListener) {
            this.negative = negative;
            this.negativeColor = negativeColor;
            this.negativeCallbackListener = negativeCallbackListener;
            return this;
        }
//        public Builder setStyle(int themeResId) {
//            this.themeResId = themeResId;
//            return this;
//        }

        public Builder setType(int type) {
            if (type == LOADING) {
                themeResId = R.style.transparent_dialog;
                isOutTouch = false;
            }
            this.type = type;
            return this;
        }

        public Dialog creat() {
            if (context == null) {
                new Throwable("Dialog context is null");
                return null;
            }
            final Dialog dialog = new Dialog(context, themeResId);

            View contentView = null;

            switch (type) {
                case DEFAULT:
                    contentView = LayoutInflater.from(context).inflate(R.layout.dialog_prompt, null);
                    break;
                case LOADING:
                    contentView = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null);
                    break;
            }

            if (contentView == null) {
                return null;
            }

            //设置内容
            TextView messageTv = (TextView) contentView.findViewById(R.id.message);
            if (messageTv != null) {
                if (message != null) {
                    messageTv.setText(message);
                }
            }

            //设置确认按钮
            TextView positiveBtn =  contentView.findViewById(R.id.btn_positive);
            if (positiveBtn != null) {
                if (positive != null) {
                    positiveBtn.setText(positive);
                }
                if (positiveColor != null) {
                    positiveBtn.setTextColor(context.getResources().getColor(positiveColor));
                }
                positiveBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (positiveCallbackListener != null) {
                            positiveCallbackListener.onClick(v);
                        }
                        dialog.dismiss();
                    }
                });
            }

            //设置取消按钮
            TextView negativeBtn = contentView.findViewById(R.id.btn_negative);
            if (onlyPositive) {
                negativeBtn.setVisibility(View.GONE);
            }
            if (negativeBtn != null) {
                if (negative != null) {
                    negativeBtn.setText(negative);
                }
                if (negativeColor != null) {
                    negativeBtn.setTextColor(context.getResources().getColor(negativeColor));
                }
                negativeBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (negativeCallbackListener != null) {
                            negativeCallbackListener.onClick(v);
                        }
                        dialog.dismiss();
                    }
                });
            }
            //其他设置
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //设置没有标题
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setContentView(contentView);//设置内容
            dialog.setCanceledOnTouchOutside(true);//设置Dialog以外是否可触摸

            return dialog;
        }
    }

    public interface CallbackListener {
        void onClick(View v);
    }
}
